﻿using System;
using NPMonitor.Attributes;
using NPMonitor.DataModel;
using NPMonitor.Interfaces;
using NPMonitor.Utility;

namespace Sharepoint
{
    public class RunJob : WebJobBase
    {
        protected override void ExecuteJob(HttpWatch.Plugin plugin)
        {
            WebDriverHelper.WebDriver = WebDriver;
            WebDriverHelper.DefaultTimeoutSecs = DefaultTimeoutSecs;

            Console.WriteLine("Starting Transactie 1 ");
            Transactie1(plugin);
            Console.WriteLine("Transactie 1 - " + " Finished");

            Console.WriteLine("Starting Transactie 2 ");
            Transactie2(plugin);
            Console.WriteLine("Transactie 2 - " + " Finished");

            Console.WriteLine("Starting Transactie 3 ");
            Transactie3(plugin);
            Console.WriteLine("Transactie 3 - " + " Finished");

            Console.WriteLine("Starting Transactie 4 ");
            Transactie4(plugin);
            Console.WriteLine("Transactie 4 - " + " Finished");
        }

        [Scenario("Sharepoint_1", "Sharepoint_Transactie_1", "Transactie 1", null)]
        public void Transactie1(HttpWatch.Plugin plugin)
        {
            try
            {
                plugin.NextPageComment = "Sharepoint_1";
                WebDriver.Navigate().GoToUrl("http://intranet.my-dsm.com/English/Pages/default.aspx");
                Control.WaitEx(plugin, -1, true, true, 0);

                //Click organization
                var navMain = WebDriverHelper.GetById(WebDriver, "nav-main");
                var divDSMtn = WebDriverHelper.GetByTagName(navMain, "div");
                var divMenu = WebDriverHelper.GetByTagName(divDSMtn, "div");
                var rootUl = WebDriverHelper.GetByTagName(divMenu, "ul");
                var rootLi = WebDriverHelper.GetByTagName(rootUl, "li");
                var menuUl = WebDriverHelper.GetByTagName(rootLi, "ul");
                var menuLi = WebDriverHelper.GetWebElementsByTagName(menuUl, "li");
                menuLi[0].Click();
                Control.WaitEx(plugin, DefaultTimeoutSecs, true, true, 0);

                //selecteer “nutritional products
                var divSelect = WebDriverHelper.GetById(WebDriver, "ctl00_m_g_516e85ec_a3bf_443a_a389_7d85e8cdecfb");
                var select = WebDriverHelper.GetByTagName(divSelect, "select");
                var option = WebDriverHelper.GetWebElementsByTagName(select, "option");
                option[26].Click();
                Control.WaitEx(plugin, DefaultTimeoutSecs, true, true, 0);
            }
            catch (Exception ex)
            {
                ExecutionResult.ErrorMessage = ex.Message;
                if (ex.InnerException != null)
                {
                    ExecutionResult.ErrorMessage = ex.Message + " -----> " + ex.InnerException.Message;
                }
                ExecutionResult.ResultStatus = JobResultStatus.Fail;
            }
        }

        [Scenario("Sharepoint_2", "Sharepoint_Transactie_2", "Transactie 2", null)]
        public void Transactie2(HttpWatch.Plugin plugin)
        {
            try
            {
                plugin.NextPageComment = "Sharepoint_2";
                WebDriver.Navigate().GoToUrl("http://intranet.my-dsm.com/English/Pages/default.aspx");
                Control.WaitEx(plugin, -1, true, true, 0);

                //Vul bij zoekveld in: “All Sites” en “vision”, klik op vergrootglas
                var navSRSB = WebDriverHelper.GetById(WebDriver, "SRSB");
                var table = WebDriverHelper.GetByTagName(navSRSB, "table");
                var td = WebDriverHelper.GetWebElementsByTagName(table, "td");
                var input = WebDriverHelper.GetByTagName(td[1], "input");
                input.Clear();
                input.SendKeys("vision");

                var linkSearch = WebDriverHelper.GetByTagName(td[2], "a");
                linkSearch.Click();

                Control.WaitEx(plugin, DefaultTimeoutSecs, true, true, 0);
            }
            catch (Exception ex)
            {
                ExecutionResult.ErrorMessage = ex.Message;
                if (ex.InnerException != null)
                {
                    ExecutionResult.ErrorMessage = ex.Message + " -----> " + ex.InnerException.Message;
                }
                ExecutionResult.ResultStatus = JobResultStatus.Fail;
            }
        }

        [Scenario("Sharepoint_3", "Sharepoint_Transactie_3", "Transactie 3", null)]
        public void Transactie3(HttpWatch.Plugin plugin)
        {
            try
            {
                plugin.NextPageComment = "Sharepoint_3";
                WebDriver.Navigate().GoToUrl("http://intranet.my-dsm.com/English/Pages/default.aspx");
                Control.WaitEx(plugin, -1, true, true, 0);

                //Click my organization (op basis van een DSM ICT AD account)
                var navMain = WebDriverHelper.GetById(WebDriver, "nav-main");
                var divDSMtn = WebDriverHelper.GetByTagName(navMain, "div");
                var divMenu = WebDriverHelper.GetByTagName(divDSMtn, "div");
                var rootUl = WebDriverHelper.GetByTagName(divMenu, "ul");
                var rootLi = WebDriverHelper.GetByTagName(rootUl, "li");
                var menuUl = WebDriverHelper.GetByTagName(rootLi, "ul");
                var menuLi = WebDriverHelper.GetWebElementsByTagName(menuUl, "li");
                menuLi[0].Click();
                Control.WaitEx(plugin, DefaultTimeoutSecs, true, true, 0);

                //Selecteer “Organization” en “who=who”
                var divLeftMenu = WebDriverHelper.GetById(WebDriver, "zz7_V4QuickLaunchMenu");
                WebDriver.Navigate().GoToUrl("https://intranet.my-dsm.com/org/cict/English/organization/Pages/wiw.aspx");

                ////var leftMenuUl = WebDriverHelper.GetByTagName(divLeftMenu, "ul");
                ////var leftMenuLi = WebDriverHelper.GetWebElementsByTagName(leftMenuUl, "li");
                ////var linkOrganization = WebDriverHelper.GetByTagName(leftMenuLi[3], "a");
                //var leftMenuLi = WebDriverHelper.GetWebElementsByCssSelector(divLeftMenu, "ul li");

                //((RemoteWebDriver)WebDriver).ExecuteScript("return document.getElementById('zz7_V4QuickLaunchMenu').children[0].children[0].children[3].children[1].style.display='';");


                ////var linkOrganization = WebDriverHelper.GetByTagName(leftMenuLi[3], "a");
                ////new Actions(WebDriver).MoveToElement(linkOrganization); //移动鼠标
                //var subLeftMenuLi = WebDriverHelper.GetByCssSelector(leftMenuLi[3], "ul li:first-child");
                ////var subLeftMenuUl = WebDriverHelper.GetWebElementsByTagName(leftMenuLi[3], "ul");
                ////var subLeftMenuLi = WebDriverHelper.GetWebElementsByTagName(subLeftMenuUl[0], "li");
                //var linkWhoIsWho = WebDriverHelper.GetByTagName(subLeftMenuLi, "a");
                ////var linkWhoIsWho = WebDriverHelper.GetByLinkText(leftMenuLi[3], "Who is who");
                //linkWhoIsWho.Click();

                //Kies “First name” en zoek op “marco”
                var selectWho = WebDriverHelper.GetById(WebDriver, "ctl00_m_g_af6eccb2_7539_4507_b2ac_0da661c254a3_QueryResultToolBar_RptControls_ddlSearch");
                var optionWho = WebDriverHelper.GetWebElementsByTagName(selectWho, "option");
                optionWho[1].Click();

                var inputSearch = WebDriverHelper.GetById(WebDriver, "ctl00_m_g_af6eccb2_7539_4507_b2ac_0da661c254a3_QueryResultToolBar_RptControls_txtSearch");
                inputSearch.Clear();
                inputSearch.SendKeys("marco");

                var buttonSearch = WebDriverHelper.GetById(WebDriver, "ctl00_m_g_af6eccb2_7539_4507_b2ac_0da661c254a3_QueryResultToolBar_RptControls_btnSearch");
                buttonSearch.Click();

                Control.WaitEx(plugin, DefaultTimeoutSecs, true, true, 0);
            }
            catch (Exception ex)
            {
                ExecutionResult.ErrorMessage = ex.Message;
                if (ex.InnerException != null)
                {
                    ExecutionResult.ErrorMessage = ex.Message + " -----> " + ex.InnerException.Message;
                }
                ExecutionResult.ResultStatus = JobResultStatus.Fail;
            }
        }

        [Scenario("Sharepoint_4", "Sharepoint_Transactie_4", "Transactie 4", null)]
        public void Transactie4(HttpWatch.Plugin plugin)
        {
            try
            {
                plugin.NextPageComment = "Sharepoint_4";
                WebDriver.Navigate().GoToUrl("http://intranet.my-dsm.com/English/Pages/default.aspx");
                Control.WaitEx(plugin, -1, true, true, 0);

                //Klik op Sitemap
                var linkSitemap = WebDriverHelper.GetById(WebDriver, "ctl00_rlSitemap");
                linkSitemap.Click();

                //Klik Corporate Communications
                var linkCorporat = WebDriverHelper.GetById(WebDriver, "ctl00_m_g_54c9306d_151d_42fc_ab21_b64feb695752_ctl03n2");
                linkCorporat.Click();

                //Klik Branding and design
                var linkBranding = WebDriverHelper.GetById(WebDriver, "ctl00_m_g_54c9306d_151d_42fc_ab21_b64feb695752_ctl03t37");
                linkBranding.Click();

                Control.WaitEx(plugin, DefaultTimeoutSecs, true, true, 0);
            }
            catch (Exception ex)
            {
                ExecutionResult.ErrorMessage = ex.Message;
                if (ex.InnerException != null)
                {
                    ExecutionResult.ErrorMessage = ex.Message + " -----> " + ex.InnerException.Message;
                }
                ExecutionResult.ResultStatus = JobResultStatus.Fail;
            }
        }

    }
}
